﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Json;
using YaAppoint.Response;

namespace YaAppoint.Appointments.CheckIn
{

    /// <summary>
    /// 服务模板
    /// 通过API签到r
    /// </summary>
    public class CheckInByApi : ICheckInByApi
    {
        private readonly IHttpClientFactory _httpClientFactory;
        private readonly HttpClient _httpClient;
        private readonly IJsonSerializer _jsonSerializer;
        private readonly IRepository<Appointment, Guid> _appointmentRepository;

        public CheckInByApi(IHttpClientFactory httpClientFactory,
                            IJsonSerializer jsonSerializer,
                            IRepository<Appointment, Guid> appointmentRepository)
        {
            _httpClientFactory = httpClientFactory;
            _httpClient = _httpClientFactory!.CreateClient();
            _httpClient.BaseAddress = new Uri("https://www.baidu.com");
            _jsonSerializer = jsonSerializer;
            _appointmentRepository = appointmentRepository;
        }

        /// <summary>
        /// 批量自动签到
        /// </summary>
        /// <param name="appointNumbers"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<BackJson> BatchCheckInAsync(List<string> appointNumbers)
        {
            return new BackJson();
        }

        /// <summary>
        /// 单个签到
        /// </summary>
        public async Task<RomoteCheckInRes> CheckInAsync(string appointNumber)
        {
            //try
            //{
            //    // 数据序列化
            //    var content = new StringContent(
            //        _jsonSerializer.Serialize(appointIds),
            //        Encoding.UTF8,
            //        "application/json"); //媒体类型

            //    // 发送请求
            //    var response = await _httpClient.PostAsync("/api/check-in", content);

            //    var pacsRes = await  response.Content.ReadFromJsonAsync<BackJson>();
            //    if (pacsRes.code == 0)
            //    {
            //        var list = await _appointmentRepository.GetListAsync(a => appointIds.Contains(a.Id));
            //        foreach (var appoint in list)
            //        {
            //            appoint.Status = AppointmentStatus.Achieve;
            //        }
            //        return new BackJson
            //        {
            //            code = 0,
            //            data = pacsRes.data
            //        };
            //    }
            //    else
            //    {
            //        return new BackJson
            //        {
            //            code = pacsRes.code,
            //            err = pacsRes.err
            //        };
            //    }
            //}
            //catch (Exception ex)
            //{
            //    throw new BusinessException(message:ex.Message);
            //}
            #region 模拟数据 
            List<RomoteCheckInRes> resList = new List<RomoteCheckInRes>();
            var res = new RomoteCheckInRes();
            res.Succeed = true;
            res.AppointNumber = appointNumber;
            res.QueueData = new
            {
                number = appointNumber,
                queque = "01",
                room= "DR1室",
                name= "张三",
                phone="18914894368"
            };
            return res;
            #endregion
        }

        /// <summary>
        /// 重新打印
        /// </summary>
        public async Task<BackJson> Reprint(string appointNumber)
        {
            return new BackJson();
        }
    }
}
